#include "../include/User.h"
#include <fstream>
#include <ios>
unsigned Admin::count = 0;

void User::add_good(const good &g, cart *a) {
  if (a == nullptr) {
    user_cart.add_good(g);
  } else {
    return;
  }
}

int User::del_good(const good &g, int num, cart *a) {
  if (a == nullptr) {
    return user_cart.del_good(g, num);
  } else {
    return 0;
  }
}

int User::del_good(int index, cart *a) {
  if (a == nullptr) {
    return user_cart.del_good(user_cart[index], user_cart[index].quantity);
  } else {
    return 0;
  }
}

Admin *Admin::admin_login(std::string name, std::string psw) {
  if (name == ADMIN && psw == ADMIN_PSW && !count) {
    return new Admin(name, psw);
  } else {
    return nullptr;
  }
}

void User::from_file(std::ifstream &fs) {
  fs >> name >> keys.first >> keys.second >> money;
}

void User::to_file(std::ofstream &fs) {
  fs << name << '\t' << keys.first << '\t' << keys.second << '\t' << money
     << '\n';
}

int Admin::login(const std::string &name, const std::string &psw) {
  if (count) {
    return -1;
  } else if (name != ADMIN || psw != ADMIN_PSW) {
    return -2;
  } else {
    return 0;
  }
}

void Admin::add_good(const good &g, cart *a) {
  if (!a) {
    return;
  } else {
    a->add_good(g);
  }
}

int Admin::del_good(const good &g, int num, cart *a) {
  if (!a) {
    return -3;
  } else {
    return a->del_good(g);
  }
}

std::ostream &operator<<(std::ostream &os, const User &cl) {
  return os << cl.name << '\t' << cl.keys.first << '\t' << cl.keys.second
            << '\t' << std::fixed << cl.money;
}